package com.xdy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdy.reggie.entity.Employee;
import com.xdy.reggie.mapper.EmployeeMapper;
import com.xdy.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Override
    public Employee login(Employee employee) {
        String password = employee.getPassword();
        String cipherText = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = baseMapper.selectOne(queryWrapper);
        //没找到用户
        if (one == null){
            log.info("用户不存在！");
            return null;
        }
        //密码不一致
        if (!cipherText.equals(one.getPassword())){
            log.info("密码错误！");
            return null;
        }
        return one;
    }
}
